package com.woldier.check.controller;

import com.alibaba.fastjson.JSONObject;
import com.woldier.check.common.hadoop.FileStatuses;
import com.woldier.check.common.R;
import com.woldier.check.common.restTemplate.RequestOption;
import com.woldier.check.common.restTemplate.CommonRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * 有关repository的接口
 */
@RestController
@RequestMapping("/repo")
public class RepositoryController {
    /**
     * 获取用户的所有仓库
     * @param username
     * @return
     */
    @Value("${wd.hadoop.proxy.url}")
    private String url;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("list")
    public R getRepositoryList(@RequestParam String username){
        /**
         *发起请求
         */
        JSONObject object = restTemplateFileStatusesJSON(username);

        /**
         * json解析
         */
        FileStatuses fileStatuses = CommonRequest.JSONObject2FileStatuses(object);
        return R.success(fileStatuses);
    }

    private  JSONObject restTemplateFileStatusesJSON(String username) {
        JSONObject object = restTemplate.getForObject(CommonRequest.generateUrl(url, username, RequestOption.LISTSTATUS),JSONObject.class);
        return object;
    }

}
